# coding: utf-8
import requests
import re
import time
from lxml import etree
from multiprocessing.dummy import Pool as ThreadPool


def changepage(url, total):
    urls = []
    now_page = int(re.search('(\d+)', url, re.S).group(1))
    for i in range(now_page, total + 1):
        link = re.sub('page_index=(\d+)', 'page_index=%s' % i, url, re.S)
        urls.append(link)
    return urls


def spider(url):
    html = requests.get(url)
    content = html.text

    selector = etree.HTML(content)
    title = []
    title = selector.xpath('//*[@id="component_0__0__6612"]/li/a/@title')

    detail = []
    detail = selector.xpath('//*[@id="component_0__0__6612"]/li/p[3]/span[1]/text()')
    save_info(title, detail)


def save_info(title, detail):
    length1 = len(title)
    for i in range(0, length1-1):
        f.writelines(title[i] + '\n')
        f.writelines(detail[i] + '\n\n')


if __name__ == '__main__':
    pool = ThreadPool(4)
    with open('D:/pythonbook.txt', 'a', encoding='utf8') as f:
        url = 'http://search.dangdang.com/?key=python&act=input&page_index=1'
        urls = changepage(url, 21)

        time1 = time.time()
        pool.map(spider, urls)
        pool.close()
        pool.join()

        f.close()
        print('爬取成功！')
        time2 = time.time()
        print('多线程耗时 ：' + str(time2 - time1) + 's')


